<template>
  <el-aside :width="$store.state.isCollapse? '180px':'64px'">
    <el-menu class="el-menu-vertical-demo"
             background-color="#545c64"
             text-color="#fff"
             :collapse="!$store.state.isCollapse"
             :collapse-transition="false">
      <h3 v-show="$store.state.isCollapse">后台管理</h3>
      <h3 v-show="!$store.state.isCollapse">后台</h3>

      <el-menu-item v-for="item1 in noChildren()"
                    :index="item1.path"
                    :key="item1.path"
                    @click="clickMenu(item1)">

        <component class="icons"
                   :is="item1.icon"> </component>
        <span>{{item1.label}}</span>
      </el-menu-item>

      <el-sub-menu v-for="item in hasChildren()"
                   :index="item.label"
                   :key="item.key">
        <template #title>
          <component class="icons"
                     :is="item.icon"> </component>
          <span>{{item.label}}</span>
        </template>

        <el-menu-item-group>
          <el-menu-item v-for="(subItem,subIndex) in item.children"
                        :index="subItem.path"
                        :key="subIndex"
                        @click="clickMenu(subItem)">
            <component class="icons"
                       :is="subItem.icon"> </component>
            <span>{{subItem.label}}</span>
          </el-menu-item>
        </el-menu-item-group>

      </el-sub-menu>

    </el-menu></el-aside>
</template>

<script>
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';

export default {
  setup () {
    const store = useStore();
    const router = useRouter();
    //有子菜单
    const noChildren = () => {
      return asyncList.filter((item) => !item.children)
    };
    //无子菜单
    const hasChildren = () => {
      return asyncList.filter((item) => item.children)
    };

    const asyncList = store.state.menu;


    //路由跳转
    const clickMenu = function (item) {
      router.push({
        name: item.name,
      });

      //vuex 来管理
      store.commit("selectMenu", item);
    };

    return {
      clickMenu,
      hasChildren,
      noChildren,


    }
  }
}
</script>

<style lang="less" scoped>
.icons {
  width: 18px;
  height: 18px;
}
.el-menu {
  border-right: none;
  h3 {
    line-height: 48px;
    color: #fff;
    text-align: center;
  }
}
</style>